package com.itfreer.data.syn;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.data.syn.dao.DataSynLogDao;
import com.itfreer.data.syn.entity.DataSynLogEntity;
import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.BaseServiceImp;

/**
 * 数据同步日志服务
 */
@Component
public class DataSynLogServiceImp extends BaseServiceImp<DataSynLogEntity> implements DataSynLogService {

	@Autowired
	private DataSynLogDao dao;

	@Override
	protected BaseDao<DataSynLogEntity> getDao() {
		return dao;
	}

	@Override
	public void deleteLogs(Integer days) {
		Date nowDate = new Date();
		nowDate = delDate(nowDate, days);
		Map<String, Object> where = new HashMap<>();
		where.put("addDate:<", nowDate);
		dao.deletes(where);
	}

	private static Date delDate(Date date, long day) {
		long time = date.getTime(); // 得到指定日期的毫秒数
		day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
		time -= day; // 相加得到新的毫秒数
		return new Date(time); // 将毫秒数转换成日期
	}
	
}